package com.aiyige.model.eshop;

import android.os.Parcel;
import android.os.Parcelable;

import com.aiyige.model.AgreementBackup;
import com.aiyige.model.request.OrderStatusEnum;
import com.aiyige.utils.ListUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuyakun on 2018/1/29.
 */

public class Order implements Parcelable{


    private Order(Builder builder) {
        setId(builder.id);
        setCode(builder.code);
        setBuyerId(builder.buyerId);
        setSellerId(builder.sellerId);
        setStatus(builder.status);
        setProductsBackup(builder.productsBackup);
        setSubOrdersBackup(builder.subOrdersBackup);
        setCreateTime(builder.createTime);
        setUpdateTime(builder.updateTime);
        setAmount(builder.amount);
        setPayAmount(builder.payAmount);
        setSource(builder.source);
        setSourceUrl(builder.sourceUrl);
        setPayMethod(builder.payMethod);
        setPayCode(builder.payCode);
        setPayTime(builder.payTime);
        setBuyerBackup(builder.buyerBackup);
        setBuyerHided(builder.buyerHided);
        setSellerHided(builder.sellerHided);
        setRouter(builder.router);
        setOrderPayBackup(builder.orderPayBackup);
        setSellerBackup(builder.sellerBackup);
        setDiscountsBackup(builder.discountsBackup);
        setCount(builder.count);
        setMomentId(builder.momentId);
        setOperations(builder.operations);
        setRefundId(builder.refundId);
        setType(builder.type);
        setRefundStatus(builder.refundStatus);
        setUserFee(builder.userFee);
        setAgreementBackup(builder.agreementBackup);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getType() {
        return type == null ? "" : type;
    }

    public void setType(String type) {
        this.type = type == null ? "" : type;
    }

    public List<DiscountsBackup> getDiscountsBackup() {
        return discountsBackup;
    }

    public void setDiscountsBackup(List<DiscountsBackup> discountsBackup) {
        this.discountsBackup = discountsBackup;
    }

    /**
     * 状态
     */
    public static class Status implements Parcelable{
        //订单状态：0-待付款，1-已付款/已完成，2-已取消，3-待发货，4-待确认，5-已关闭，6-申请退货，7-已退货，8-退款中，9-已退款
        public static final int UNPAY = 0;
        public static final int PAYED = 1;
        public static final int CANCELED = 2;
        public static final int UNSHIPPING = 3;
        public static final int UNCONFIRM = 4;
        public static final int CONFIRMED = 5;
        public static final int TOREFUND = 6;
        public static final int RETURNED = 7;
        public static final int REFUNDEDING = 8;
        public static final int REFUNDED = 9;

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
        }

        public Status() {
        }

        protected Status(Parcel in) {
        }

        public static final Creator<Status> CREATOR = new Creator<Status>() {
            @Override
            public Status createFromParcel(Parcel source) {
                return new Status(source);
            }

            @Override
            public Status[] newArray(int size) {
                return new Status[size];
            }
        };
    }

    /**
     * 范围
     */
    public static class Range implements Parcelable{

        public static final String ALL = "all";//全部
        public static final String NOPAY = "nopay";//未付款
        public static final String CANCELLED = "cancelled";//已取消
        public static final String NOSHIP = "noship";//待服务/发货
        public static final String UNCONFIRM = "unconfirm";//待确认
        public static final String AFTERSALE = "aftersale";//退款/售后

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
        }

        public Range() {
        }

        protected Range(Parcel in) {
        }

        public static final Creator<Range> CREATOR = new Creator<Range>() {
            @Override
            public Range createFromParcel(Parcel source) {
                return new Range(source);
            }

            @Override
            public Range[] newArray(int size) {
                return new Range[size];
            }
        };
    }
    public Order() {

    }


    public static String getOrderStatusValue(int orderStatus) {
        if (orderStatus == OrderStatusEnum.STATUS_0_NOPAY.getStatus()) {
            return Range.NOPAY;
        } else if (orderStatus == OrderStatusEnum.STATUS_2_CANCEL.getStatus()) {
            return Range.CANCELLED;
        } else if (orderStatus == OrderStatusEnum.STATUS_3_WAITING_SHIP.getStatus()) {
            return Range.NOSHIP;
        } else if (orderStatus == OrderStatusEnum.STATUS_4_WAITING_AFFIRM.getStatus()) {
            return Range.UNCONFIRM;
        } else if (orderStatus == OrderStatusEnum.STATUS_6_SALES_RETURN.getStatus()) {
            return Range.AFTERSALE;
        } else {
            return "";
        }
    }

    /**
     * 范围
     */
    public static class Time {

        public static final String THREE_DAY = "threeday";//三天内
        public static final String ONE_WEEK = "oneweek";//三天内
        public static final String TWO_WEEK = "twoweek";//两周内
        public static final String ONE_MONTH = "onemonth";//一个月内
        public static final String TWO_MONTH = "twomonth";//两个月内

    }

    public static final int PAY_METHOD_NONE = -1;
    public static final int PAY_METHOD_ALIPAY = 1;
    public static final int PAY_METHOD_WECHATPAY = 2;
    public static final int PAY_METHOD_BANK_CARD = 3;
    public static final int PAY_METHOD_APPLE_PAY = 4;
    public static final int PAY_METHOD_ALIPAY_APP = 5;
    public static final int PAY_METHOD_WECHATPAY_APP = 6;
    public static final int PAY_METHOD_BALANCE = 11;


    String id;
    String code;
    String buyerId;
    String sellerId;
    Integer status;
    List<ProductsBackup> productsBackup;
    List<Order> subOrdersBackup;
    long createTime;
    long updateTime;
    double amount;
    double payAmount;
    String source;
    String sourceUrl;
    Integer payMethod;
    String payCode;
    long payTime;
    BuyerBackup buyerBackup;
    Integer buyerHided;
    Integer sellerHided;
    String router;
    OrderPay orderPayBackup;
    SellerBackup sellerBackup;
    List<DiscountsBackup> discountsBackup;

    int count;// 这个字段是订单修改的时候用的

    String momentId;//这个字段是订单修改的时候用的

    List<String> operations;// 操作

    String refundId;// 退款id，用来查询退款详情

    String type = "pingPlus";// 后台要求写死这个值

    public String getRefundId() {
        return refundId == null ? "" : refundId;
    }

    public void setRefundId(String refundId) {
        this.refundId = refundId == null ? "" : refundId;
    }

    public String getRefundStatus() {
        return refundStatus == null ? "" : refundStatus;
    }

    public void setRefundStatus(String refundStatus) {
        this.refundStatus = refundStatus == null ? "" : refundStatus;
    }

    String refundStatus;

    public double getUserFee() {
        return userFee;
    }

    public void setUserFee(double userFee) {
        this.userFee = userFee;
    }

    double userFee;

    public List<String> getOperations() {
        return operations;
    }

    public void setOperations(List<String> operations) {
        this.operations = operations;
    }

    public void setMomentId(String momentId) {
        this.momentId = momentId;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }


    public List<AgreementBackup> getAgreementBackup() {
        if (agreementBackup == null) {
            return new ArrayList<>();
        }
        return agreementBackup;
    }

    public void setAgreementBackup(List<AgreementBackup> agreementBackup) {
        this.agreementBackup = agreementBackup;
    }

    List<AgreementBackup> agreementBackup;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getBuyerId() {
        return buyerId;
    }

    public void setBuyerId(String buyerId) {
        this.buyerId = buyerId;
    }

    public String getSellerId() {
        return sellerId;
    }

    public void setSellerId(String sellerId) {
        this.sellerId = sellerId;
    }

    public Integer getStatus() {
        return status;
    }

    public String getStatusName() {
        return OrderStatusEnum.getValueByKey(status);
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public List<ProductsBackup> getProductsBackup() {
        return productsBackup;
    }

    public void setProductsBackup(List<ProductsBackup> productsBackup) {
        this.productsBackup = productsBackup;
    }

    public List<Order> getSubOrdersBackup() {
        return subOrdersBackup;
    }

    public void setSubOrdersBackup(List<Order> subOrdersBackup) {
        this.subOrdersBackup = subOrdersBackup;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(long updateTime) {
        this.updateTime = updateTime;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public double getPayAmount() {
        return payAmount;
    }

    public void setPayAmount(double payAmount) {
        this.payAmount = payAmount;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getSourceUrl() {
        return sourceUrl != null ? sourceUrl : "";
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    public Integer getPayMethod() {
        return payMethod;
    }

    public void setPayMethod(Integer payMethod) {
        this.payMethod = payMethod;
    }

    public String getPayCode() {
        return payCode;
    }

    public void setPayCode(String payCode) {
        this.payCode = payCode;
    }

    public long getPayTime() {
        return payTime;
    }

    public void setPayTime(long payTime) {
        this.payTime = payTime;
    }

    public BuyerBackup getBuyerBackup() {
        return buyerBackup;
    }

    public void setBuyerBackup(BuyerBackup buyerBackup) {
        this.buyerBackup = buyerBackup;
    }

    public Integer getBuyerHided() {
        return buyerHided;
    }

    public void setBuyerHided(Integer buyerHided) {
        this.buyerHided = buyerHided;
    }

    public Integer getSellerHided() {
        return sellerHided;
    }

    public void setSellerHided(Integer sellerHided) {
        this.sellerHided = sellerHided;
    }

    public String getRouter() {
        return router;
    }

    public void setRouter(String router) {
        this.router = router;
    }

    public OrderPay getOrderPayBackup() {
        return orderPayBackup;
    }

    public void setOrderPayBackup(OrderPay orderPayBackup) {
        this.orderPayBackup = orderPayBackup;
    }

    public SellerBackup getSellerBackup() {
        return sellerBackup;
    }

    public void setSellerBackup(SellerBackup sellerBackup) {
        this.sellerBackup = sellerBackup;
    }


    public String getMomentId() {
        if (!ListUtil.isEmpty(getProductsBackup())) {
            return getProductsBackup().get(0).getMomentId();
        }
        return null;
    }





    public double getCharge() {
        return payAmount - userFee;
    }


    public static final class Builder {
        private String id;
        private String code;
        private String buyerId;
        private String sellerId;
        private Integer status;
        private List<ProductsBackup> productsBackup;
        private List<Order> subOrdersBackup;
        private long createTime;
        private long updateTime;
        private double amount;
        private double payAmount;
        private String source;
        private String sourceUrl;
        private Integer payMethod;
        private String payCode;
        private long payTime;
        private BuyerBackup buyerBackup;
        private Integer buyerHided;
        private Integer sellerHided;
        private String router;
        private OrderPay orderPayBackup;
        private SellerBackup sellerBackup;
        private List<DiscountsBackup> discountsBackup;
        private int count;
        private String momentId;
        private List<String> operations;
        private String refundId;
        private String type;
        private String refundStatus;
        private double userFee;
        private List<AgreementBackup> agreementBackup;

        private Builder() {
        }

        public Builder id(String val) {
            id = val;
            return this;
        }

        public Builder code(String val) {
            code = val;
            return this;
        }

        public Builder buyerId(String val) {
            buyerId = val;
            return this;
        }

        public Builder sellerId(String val) {
            sellerId = val;
            return this;
        }

        public Builder status(Integer val) {
            status = val;
            return this;
        }

        public Builder productsBackup(List<ProductsBackup> val) {
            productsBackup = val;
            return this;
        }

        public Builder subOrdersBackup(List<Order> val) {
            subOrdersBackup = val;
            return this;
        }

        public Builder createTime(long val) {
            createTime = val;
            return this;
        }

        public Builder updateTime(long val) {
            updateTime = val;
            return this;
        }

        public Builder amount(double val) {
            amount = val;
            return this;
        }

        public Builder payAmount(double val) {
            payAmount = val;
            return this;
        }

        public Builder source(String val) {
            source = val;
            return this;
        }

        public Builder sourceUrl(String val) {
            sourceUrl = val;
            return this;
        }

        public Builder payMethod(Integer val) {
            payMethod = val;
            return this;
        }

        public Builder payCode(String val) {
            payCode = val;
            return this;
        }

        public Builder payTime(long val) {
            payTime = val;
            return this;
        }

        public Builder buyerBackup(BuyerBackup val) {
            buyerBackup = val;
            return this;
        }

        public Builder buyerHided(Integer val) {
            buyerHided = val;
            return this;
        }

        public Builder sellerHided(Integer val) {
            sellerHided = val;
            return this;
        }

        public Builder router(String val) {
            router = val;
            return this;
        }

        public Builder orderPayBackup(OrderPay val) {
            orderPayBackup = val;
            return this;
        }

        public Builder sellerBackup(SellerBackup val) {
            sellerBackup = val;
            return this;
        }

        public Builder discountsBackup(List<DiscountsBackup> val) {
            discountsBackup = val;
            return this;
        }

        public Builder count(int val) {
            count = val;
            return this;
        }

        public Builder momentId(String val) {
            momentId = val;
            return this;
        }

        public Builder operations(List<String> val) {
            operations = val;
            return this;
        }

        public Builder refundId(String val) {
            refundId = val;
            return this;
        }

        public Builder type(String val) {
            type = val;
            return this;
        }

        public Builder refundStatus(String val) {
            refundStatus = val;
            return this;
        }

        public Builder userFee(double val) {
            userFee = val;
            return this;
        }

        public Builder agreementBackup(List<AgreementBackup> val) {
            agreementBackup = val;
            return this;
        }

        public Order build() {
            return new Order(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.code);
        dest.writeString(this.buyerId);
        dest.writeString(this.sellerId);
        dest.writeValue(this.status);
        dest.writeTypedList(this.productsBackup);
        dest.writeTypedList(this.subOrdersBackup);
        dest.writeLong(this.createTime);
        dest.writeLong(this.updateTime);
        dest.writeDouble(this.amount);
        dest.writeDouble(this.payAmount);
        dest.writeString(this.source);
        dest.writeString(this.sourceUrl);
        dest.writeValue(this.payMethod);
        dest.writeString(this.payCode);
        dest.writeLong(this.payTime);
        dest.writeParcelable(this.buyerBackup, flags);
        dest.writeValue(this.buyerHided);
        dest.writeValue(this.sellerHided);
        dest.writeString(this.router);
        dest.writeParcelable(this.orderPayBackup, flags);
        dest.writeParcelable(this.sellerBackup, flags);
        dest.writeTypedList(this.discountsBackup);
        dest.writeInt(this.count);
        dest.writeString(this.momentId);
        dest.writeStringList(this.operations);
        dest.writeString(this.refundId);
        dest.writeString(this.type);
        dest.writeString(this.refundStatus);
        dest.writeDouble(this.userFee);
        dest.writeTypedList(this.agreementBackup);
    }

    protected Order(Parcel in) {
        this.id = in.readString();
        this.code = in.readString();
        this.buyerId = in.readString();
        this.sellerId = in.readString();
        this.status = (Integer) in.readValue(Integer.class.getClassLoader());
        this.productsBackup = in.createTypedArrayList(ProductsBackup.CREATOR);
        this.subOrdersBackup = in.createTypedArrayList(Order.CREATOR);
        this.createTime = in.readLong();
        this.updateTime = in.readLong();
        this.amount = in.readDouble();
        this.payAmount = in.readDouble();
        this.source = in.readString();
        this.sourceUrl = in.readString();
        this.payMethod = (Integer) in.readValue(Integer.class.getClassLoader());
        this.payCode = in.readString();
        this.payTime = in.readLong();
        this.buyerBackup = in.readParcelable(BuyerBackup.class.getClassLoader());
        this.buyerHided = (Integer) in.readValue(Integer.class.getClassLoader());
        this.sellerHided = (Integer) in.readValue(Integer.class.getClassLoader());
        this.router = in.readString();
        this.orderPayBackup = in.readParcelable(OrderPay.class.getClassLoader());
        this.sellerBackup = in.readParcelable(SellerBackup.class.getClassLoader());
        this.discountsBackup = in.createTypedArrayList(DiscountsBackup.CREATOR);
        this.count = in.readInt();
        this.momentId = in.readString();
        this.operations = in.createStringArrayList();
        this.refundId = in.readString();
        this.type = in.readString();
        this.refundStatus = in.readString();
        this.userFee = in.readDouble();
        this.agreementBackup = in.createTypedArrayList(AgreementBackup.CREATOR);
    }

    public static final Creator<Order> CREATOR = new Creator<Order>() {
        @Override
        public Order createFromParcel(Parcel source) {
            return new Order(source);
        }

        @Override
        public Order[] newArray(int size) {
            return new Order[size];
        }
    };
}
